package com.example.mq;

import com.example.constant.CommentConstant;
import com.example.entity.OrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 订单生产者
 * @Author: mingtian
 * @CreateDate: 2021/1/21 15:55
 * @Version: 1.0
 */
@RestController
public class OrderProducer {
    /**
     * 打印日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送 字符串方式消息
     *
     * @return
     */
    @RequestMapping("/sendOrderString")
    public String sendOrder() {
        String msg = "一台Mate40Pro+价格为8999";
        // 参数1 交换机名称 、参数2路由key  参数3 消息
        amqpTemplate.convertAndSend(CommentConstant.ORDER_EXCHANGE_NAME, CommentConstant.ORDER_ROUTING_KEY, msg);
        logger.info("订单队列投递消息成功");
        return "success";
    }

    /**
     * 发送对象方式消息
     *
     * @return
     */
    @RequestMapping("/sendOrderInfo")
    public String sendOrderInfo() {
        OrderInfo orderInfo = new OrderInfo(System.currentTimeMillis() + "", "华为Mate40Pro+");
        // 参数1 交换机名称 、参数2路由key  参数3 消息
        amqpTemplate.convertAndSend(CommentConstant.ORDER_EXCHANGE_NAME, CommentConstant.ORDER_ROUTING_KEY, orderInfo);
        return "success";
    }
}
